package fwhmc.registrar;

import fwhmc.annotation.EnableWebFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 类描述: Http插件注册处理
 *
 * @author : 王雷
 * @date : 2020/3/21 0021 下午 6:34
 */
@Slf4j
public class HttpPlusImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //这里获取通过@ControolerScan配置的路径信息
        AnnotationAttributes mapperScanAttrs = AnnotationAttributes
                .fromMap(importingClassMetadata.getAnnotationAttributes(EnableWebFilter.class.getName()));
        assert mapperScanAttrs != null;
        final Class<? extends ErrorWebExceptionHandler> errorWebExceptionBean = mapperScanAttrs
                .getClass("errorWebExceptionBean");
        registerBean(registry, "errorWebExceptionBean", errorWebExceptionBean);

//        final String jacksonConfigBeanPath = mapperScanAttrs.getString("jacksonConfigBeanPath");

//        //构建扫描器，开始扫描指定的路径
//        new ClassPathBeanDefinitionScanner(registry){
//            @NotNull
//            @Override
//            public Set<BeanDefinitionHolder> doScan(String @NotNull ... basePackages) {
//                return super.doScan(basePackages);
//            }
//        }.doScan(StringUtils.isNotEmpty(jacksonConfigBeanPath) ? jacksonConfigBeanPath : "fmppc.config");
    }

    /**
     * 功能描述：注册bean
     *
     * @date : 2022/6/22 0022 下午 7:17
     */
    private void registerBean(BeanDefinitionRegistry registry, String name, Class<?> beanClass) {
        RootBeanDefinition bean = new RootBeanDefinition(beanClass);
        registry.registerBeanDefinition(name, bean);
    }

}
